<?php
declare(strict_types=1);

namespace Plugin\Alarm\Repository;

use App\Repository\IRepository;
use Hyperf\Collection\Arr;
use Hyperf\Database\Model\Builder;
use Plugin\Alarm\Model\RdAlarmWork;

class RdAlarmWorkRepository extends IRepository
{
    public function __construct(protected readonly RdAlarmWork $model)
    {
    }

    public function handleSearch(Builder $query, array $params): Builder
    {
        if ($id = Arr::get($params, 'id')) {
            $query->whereIn('id', Arr::wrap($id));
        }
        if ($name = Arr::get($params, 'title')) {
            $query->where('title', 'like', "%{$name}%");
        }
        if ($status = Arr::get($params, 'status')) {
            $query->whereIn('status', Arr::wrap($status));
        }
        if ($type = Arr::get($params, 'type')) {
            $query->where('type', $type);
        }
        if ($priority = Arr::get($params, 'priority')) {
            $query->where('priority', $priority);
        }
        if ($relatedType = Arr::get($params, 'related_type')) {
            $query->where('related_type', $relatedType);
        }
        if ($standId = Arr::get($params, 'stand_id')) {
            $query->whereIn('stand_id', Arr::wrap($standId));
        }
        //
        if (isset($params['created_at']) && filled($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [$params['created_at'][0], $params['created_at'][1]]
            );
        }

        //
        if (isset($params['updated_at']) && filled($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [$params['updated_at'][0], $params['updated_at'][1]]
            );
        }

        return $query;
    }

    public function apiPage(array $params, int $page, int $pageSize, array $adminStands = [])
    {
        $result = $this->perQuery($this->getQuery(), $params);
        
        // 如果指定了站点ID
        if (isset($params['stand_id'])) {
            $standIds = Arr::wrap($params['stand_id']);
            
            // 检查是否是管理员管理的站点
            $managedStands = array_intersect($standIds, $adminStands);
            
            if (!empty($managedStands)) {
                // 如果有管理的站点，返回这些站点的所有工单
                $result = $result->whereIn('stand_id', $managedStands);
                
                // 同时也包含用户处理的其他站点工单
                $nonManagedStands = array_diff($standIds, $adminStands);
                if (!empty($nonManagedStands)) {
                    $result = $result->orWhere(function ($query) use ($nonManagedStands, $params) {
                        $query->whereIn('stand_id', $nonManagedStands)
                            ->where('user_id', $params['user_id']);
                    });
                }
            } else {
                // 如果没有管理的站点，只返回这些站点中分配给用户处理的工单
                $result = $result->whereIn('stand_id', $standIds)
                    ->where('user_id', $params['user_id']);
            }
        } else {
            // 没有指定站点时，返回用户管理的所有站点工单 + 用户处理的所有工单
            if (!empty($adminStands) || $params['user_id'] > 0) {
                $result = $result->where(function ($query) use ($adminStands, $params) {
                    if (!empty($adminStands)) {
                        $query->whereIn('stand_id', $adminStands);
                    }
                    if ($params['user_id'] > 0) {
                        $query->orWhere('user_id', $params['user_id']);
                    }
                });
            }
        }
        
        $result = $result->paginate(
                perPage: $pageSize,
                pageName: static::PER_PAGE_PARAM_NAME,
                page: $page,
            );
        return $this->handlePage($result);
    }

}